<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="填报对象" prop="reportObj">
        <el-select v-model="queryParams.reportObj" placeholder="填报对象" @change="getList" clearable>
          <el-option
            v-for="dict in dict.type.business_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="填报周期" prop="reportCycle">
        <el-select v-model="queryParams.reportCycle" placeholder="填报周期" @change="getList" clearable>
          <el-option
            v-for="dict in dict.type.filling_cycle"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人" prop="createBy" v-show="isFoldQuery">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" v-show="isFoldQuery">
        <el-date-picker
          v-model="createDateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="修改人" prop="updateBy" v-show="isFoldQuery">
        <el-input
          v-model="queryParams.updateBy"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改时间" v-show="isFoldQuery">
        <el-date-picker
          v-model="updateDateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                <el-button
          plain
          :icon="isFoldQuery ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"
          size="mini"
          @click="foldQuery"
        >
          {{ isFoldQuery ? '折叠' : '展开' }}
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['reportTask:reportTask:add']"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['reportTask:reportTask:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['reportTask:reportTask:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="reportTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键id" align="center" prop="id" v-if="false"/>
      <el-table-column label="任务名称" align="center" prop="taskName" width="180" fixed/>
      <el-table-column label="填报对象" align="center" prop="reportObj" width="180" fixed>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_type" :value="scope.row.reportObj"/>
        </template>
      </el-table-column>
      <el-table-column label="填报周期" align="center" prop="reportCycle" width="180">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.filling_cycle" :value="scope.row.reportCycle"/>
        </template>
      </el-table-column>
      <el-table-column label="推送时间" align="left" prop="sendDateStr" width="250"/>
      <!--      <el-table-column label="截止日期" align="center" prop="validityPeriod" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          -->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="有效期(天)" align="center" prop="validityPeriod" width="180"/>
      <el-table-column label="填报模板" align="center" prop="reportTemplate" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="previewTemplates(scope.row)"
          >预览
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="填报说明" align="left" prop="remark" width="250" :show-overflow-tooltip="true"/>
      <el-table-column label="创建人" align="center" prop="createBy" width="220"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="220"/>
      <el-table-column label="修改修改人" align="center" prop="updateBy" width="220"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="220"/>
      <el-table-column label="数据状态" align="center" prop="dataStatus" width="120" fixed="right">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.data_status_examine" :value="scope.row.dataStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="250">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['reportTask:reportTask:edit']"
            v-show="scope.row.dataStatus ===0 || scope.row.dataStatus ===2"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-checked"
            @click="handleUp(scope.row)"
            v-hasPermi="['reportTask:reportTask:edit']"
            v-show="scope.row.dataStatus ===0 || scope.row.dataStatus ===2"
          >提交
          </el-button>

          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-close"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['reportTask:reportTask:edit']"-->
          <!--            v-show="scope.row.dataStatus ===1"-->
          <!--          >撤回</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-remove-outline"
            @click="handleStop(scope.row)"
            v-hasPermi="['reportTask:reportTask:edit']"
            v-show="scope.row.dataStatus ===3"
          >停用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleStart(scope.row)"
            v-hasPermi="['reportTask:reportTask:edit']"
            v-show="scope.row.dataStatus ===4"
          >启用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['reportTask:reportTask:remove']"
            v-show="scope.row.dataStatus ===0 || scope.row.dataStatus ===2"
          >删除
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-check"-->
          <!--            @click="handleReviewCheck(scope.row)"-->
          <!--            v-hasPermi="['reportTask:reportTask:review']"-->
          <!--            v-show="scope.row.dataStatus ===1"-->
          <!--          >审核通过-->
          <!--          </el-button>-->
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-close"-->
          <!--            @click="handleReviewClose(scope.row)"-->
          <!--            v-hasPermi="['reportTask:reportTask:review']"-->
          <!--            v-show="scope.row.dataStatus ===1"-->
          <!--          >审核驳回-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {
  listReportTask,
  getReportTask,
  delReportTask,
  addReportTask,
  updateReportTask
} from '@/api/reportTask/reportTask'

export default {
  name: 'ReportTask',
  dicts: ['data_status_examine', 'business_type', 'filling_cycle'],
  data() {
    return {
      isFoldQuery:false,
      // 日期范围
      createDateRange: [],
      updateDateRange: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 填报任务表格数据
      reportTaskList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: undefined,
        reportObj: undefined,
        reportCycle: undefined,
        sendDate: undefined,
        validityPeriod: undefined,
        reportTemplate: undefined,
        dataStatus: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: '主键id不能为空', trigger: 'blur' }
        ],
        taskName: [
          { required: true, message: '任务名称不能为空', trigger: 'blur' }
        ],
        reportObj: [
          { required: true, message: '填报对象不能为空', trigger: 'blur' }
        ],
        reportCycle: [
          { required: true, message: '填报周期不能为空', trigger: 'blur' }
        ],
        sendDate: [
          { required: true, message: '推送时间不能为空', trigger: 'blur' }
        ],
        validityPeriod: [
          { required: true, message: '有效期不能为空', trigger: 'blur' }
        ],
        reportTemplate: [
          { required: true, message: '填报模板集合不能为空', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: '备注不能为空', trigger: 'blur' }
        ],
        dataStatus: [
          { required: true, message: '数据状态不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询填报任务列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.createDateRange && '' != this.createDateRange) {
        this.queryParams.params['beginCreateTime'] = this.createDateRange[0]
        this.queryParams.params['endCreateTime'] = this.createDateRange[1]
      }
      if (null != this.updateDateRange && '' != this.updateDateRange) {
        this.queryParams.params['beginUpdateTime'] = this.updateDateRange[0]
        this.queryParams.params['endUpdateTime'] = this.updateDateRange[1]
      }
      listReportTask(this.queryParams).then(response => {
        this.reportTaskList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮resetQuery
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        taskName: undefined,
        reportObj: undefined,
        reportCycle: undefined,
        sendDate: undefined,
        validityPeriod: undefined,
        reportTemplate: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        dataStatus: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      console.log("点击重置按钮")
      this.createDateRange = []
      this.updateDateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    foldQuery() {
      this.isFoldQuery = !this.isFoldQuery;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   this.open = true;
    //   this.title = "添加填报任务";
    // },
    handleAdd() {
      this.$router.push({
        path: '/reportTask/reportTask/edit'    // 路由地址自己确认一下
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids[0]
      this.$router.push({
        path: '/reportTask/reportTask/edit',
        query: { id }
      })
    },
    previewTemplates(row) {
      const params = new URLSearchParams({
        titleName: row.taskName,
        helperText: row.remark,
        deadline:"XXXXXXXX",
        isPreview:true,
        reportTemplate: row.reportTemplate,
      }).toString()

      window.open(`/report/previewFill/reportFill?${params}`, '_blank')
    },
    // handleUpdate(row) {
    //   this.loading = true;
    //   this.reset();
    //   const id = row.id || this.ids
    //   getReportTask(id).then(response => {
    //     this.loading = false;
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改填报任务";
    //   });
    // },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.buttonLoading = true
          if (this.form.id != null) {
            updateReportTask(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            }).finally(() => {
              this.buttonLoading = false
            })
          } else {
            addReportTask(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            }).finally(() => {
              this.buttonLoading = false
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除填报任务编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true
        return delReportTask(ids)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      }).finally(() => {
        this.loading = false
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('reportTask/reportTask/export', {
        ...this.queryParams
      }, `reportTask_${new Date().getTime()}.xlsx`)
    },
    handleUp(row) {
      this.$modal.confirm('是否确认提交？').then(() => {
        this.loading = true
        row.dataStatus = 3
        return updateReportTask(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('提交成功')
      }).catch(() => {
        this.getList()
      }).finally(() => {
        this.loading = false
      })
    },
    handleStart(row) {
      this.$modal.confirm('是否确认启用？').then(() => {
        this.loading = true
        row.dataStatus = 3
        return updateReportTask(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('启用成功')
      }).catch(() => {
        row.dataStatus = 4
      }).finally(() => {
        this.loading = false
      })
    },
    handleStop(row) {
      this.$modal.confirm('是否确认停用？').then(() => {
        this.loading = true
        row.dataStatus = 4
        return updateReportTask(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('停用成功')
      }).catch(() => {
        row.dataStatus = 3
      }).finally(() => {
        this.loading = false
      })
    },
    handleReviewCheck(row) {
      this.$modal.confirm('是否确认通过审核？').then(() => {
        this.loading = true
        row.dataStatus = 3
        return updateReportTask(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('审核通过成功')
      }).catch(() => {
        this.getList()
      }).finally(() => {
        this.loading = false
      })
    },
    handleReviewClose(row) {
      this.$modal.confirm('是否确认驳回？').then(() => {
        this.loading = true
        row.dataStatus = 2
        return updateReportTask(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('驳回成功')
      }).catch(() => {
        this.getList()
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>
